package jsu.hx.lost.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jsu.hx.lost.Mapper.NotificationsMapper;
import jsu.hx.lost.Model.entity.Notifications;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class NotificationsService
        extends ServiceImpl<NotificationsMapper, Notifications>
        {


    public IPage<Notifications> getNotificationsByUser(Long userId, Integer pageNum, Integer pageSize) {
        Page<Notifications> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectNotificationsWithDetails(page, userId);
    }

    @Transactional
    public boolean markAsRead(Long notificationId) {
        Notifications notification = getById(notificationId);
        if (notification == null) {
            throw new RuntimeException("通知不存在");
        }
        notification.setIsRead(true);
        return updateById(notification);
    }

            // NotificationsService.java
            public Notifications getByRelatedItem(Long userId, String relatedType, Long itemId) {
                QueryWrapper<Notifications> query = new QueryWrapper<>();
                query.eq("user_id", userId);
                if ("LOST".equalsIgnoreCase(relatedType)) {
                    query.eq("related_lost_item_id", itemId);
                } else if ("FOUND".equalsIgnoreCase(relatedType)) {
                    query.eq("related_found_item_id", itemId);
                } else {
                    throw new IllegalArgumentException("关联类型无效");
                }
                return getOne(query);
            }

}